<template>
  <view class="week_all">
    <view
      class="week_btn"
      :style="
        weekReservation.scheduleMondayState == 1
          ? 'background-color: #0069B6;border-color: #0069B6;color: #fff;'
          : 'background-color: #DCDCDC;border-color: #DCDCDC;color: #979797;'
      "
      @tap="clickMonday(weekReservation.scheduleMondayState)"
      >周一</view
    >
    <view
      class="week_btn"
      :style="
        weekReservation.scheduleTuesdayState == 1
          ? 'background-color: #0069B6;border-color: #0069B6;color: #fff;'
          : 'background-color: #DCDCDC;border-color: #DCDCDC;color: #979797;'
      "
      @tap="clickTuesday(weekReservation.scheduleTuesdayState)"
      >周二</view
    >
    <view
      class="week_btn"
      :style="
        weekReservation.scheduleWednesdayState == 1
          ? 'background-color: #0069B6;border-color: #0069B6;color: #fff;'
          : 'background-color: #DCDCDC;border-color: #DCDCDC;color: #979797;'
      "
      @tap="clickWednesday(weekReservation.scheduleWednesdayState)"
      >周三</view
    >
    <view
      class="week_btn"
      :style="
        weekReservation.scheduleThursdayState == 1
          ? 'background-color: #0069B6;border-color: #0069B6;color: #fff;'
          : 'background-color: #DCDCDC;border-color: #DCDCDC;color: #979797;'
      "
      @tap="clickThursday(weekReservation.scheduleThursdayState)"
      >周四</view
    >
    <view
      class="week_btn"
      :style="
        weekReservation.scheduleFridayState == 1
          ? 'background-color: #0069B6;border-color: #0069B6;color: #fff;'
          : 'background-color: #DCDCDC;border-color: #DCDCDC;color: #979797;'
      "
      @tap="clickFriday(weekReservation.scheduleFridayState)"
      >周五</view
    >
    <view
      class="week_btn"
      :style="
        weekReservation.scheduleSaturdayState == 1
          ? 'background-color: #0069B6;border-color: #0069B6;color: #fff;'
          : 'background-color: #DCDCDC;border-color: #DCDCDC;color: #979797;'
      "
      @tap="clickSaturday(weekReservation.scheduleSaturdayState)"
      >周六</view
    >
    <view
      class="week_btn"
      :style="
        weekReservation.scheduleSundayState == 1
          ? 'background-color: #0069B6;border-color: #0069B6;color: #fff;'
          : 'background-color: #DCDCDC;border-color: #DCDCDC;color: #979797;'
      "
      @tap="clickSunday(weekReservation.scheduleSundayState)"
      >周日</view
    >
  </view>
</template>

<script>
  const CmdType = {
    Booking: 7,
  }
  import getPrefixAndControl from '../../util/getPrefixAndControl'
  export default {
    props: {
      // 父级传递过来对象
      weekReservation: {
        type: Object,
      },
      equipmentNo: {
        type: String,
      },
      equipmentTypeId: {
        type: String,
      },
    },
    data() {
      return {
        //防止多次点击
        isRendering: false,
      }
    },
    methods: {
      clickMonday: async function (tag) {
        let getbookList = this.getBookingArray('Monday')
        this.preventClick(this.isRendering)
        if (tag == 0) {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        } else {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        }
      },
      //点击周二
      clickTuesday: async function (tag) {
        let getbookList = this.getBookingArray('Tuesday')
        this.preventClick(this.isRendering)
        if (tag == 0) {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        } else {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        }
      },
      //点击周三
      clickWednesday: async function (tag) {
        let getbookList = this.getBookingArray('Wednesday')
        this.preventClick(this.isRendering)
        if (tag == 0) {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        } else {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        }
      },
      //点击周四
      clickThursday: async function (tag) {
        let getbookList = this.getBookingArray('Thursday')
        this.preventClick(this.isRendering)
        if (tag == 0) {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        } else {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        }
      },
      //点击周五
      clickFriday: async function (tag) {
        let getbookList = this.getBookingArray('Friday')
        this.preventClick(this.isRendering)
        if (tag == 0) {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        } else {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        }
      },
      //点击周六
      clickSaturday: async function (tag) {
        let getbookList = this.getBookingArray('Saturday')
        this.preventClick(this.isRendering)
        if (tag == 0) {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        } else {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        }
      },
      //点击周日
      clickSunday: async function (tag) {
        let getbookList = this.getBookingArray('Sunday')
        this.preventClick(this.isRendering)
        if (tag == 0) {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        } else {
          this.$parent.loadingClick()
          const success = await getPrefixAndControl.controlCmd(
            this.equipmentTypeId,
            this.equipmentNo,
            CmdType.Booking,
            getbookList
          )
          if (success.code != 200) {
            this.$modal.msgError('下发失败')
          }
        }
      },
      //点击星期触发公共事件
      getBookingArray(week) {
        let weekState = {
          Sunday: this.weekReservation.scheduleSundayState == 0 ? false : true,
          Monday: this.weekReservation.scheduleMondayState == 0 ? false : true,
          Tuesday:
            this.weekReservation.scheduleTuesdayState == 0 ? false : true,
          Wednesday:
            this.weekReservation.scheduleWednesdayState == 0 ? false : true,
          Thursday:
            this.weekReservation.scheduleThursdayState == 0 ? false : true,
          Friday: this.weekReservation.scheduleFridayState == 0 ? false : true,
          Saturday:
            this.weekReservation.scheduleSaturdayState == 0 ? false : true,
        }
        switch (week) {
          case 'Sunday':
            weekState.Sunday =
              this.weekReservation.scheduleSundayState == 0 ? true : false
            break
          case 'Monday':
            weekState.Monday =
              this.weekReservation.scheduleMondayState == 0 ? true : false
            break
          case 'Tuesday':
            weekState.Tuesday =
              this.weekReservation.scheduleTuesdayState == 0 ? true : false
            break
          case 'Wednesday':
            weekState.Wednesday =
              this.weekReservation.scheduleWednesdayState == 0 ? true : false
            break
          case 'Thursday':
            weekState.Thursday =
              this.weekReservation.scheduleThursdayState == 0 ? true : false
            break
          case 'Friday':
            weekState.Friday =
              this.weekReservation.scheduleFridayState == 0 ? true : false
            break
          case 'Saturday':
            weekState.Saturday =
              this.weekReservation.scheduleSaturdayState == 0 ? true : false
            break
        }
        return weekState
      },
      //防止多次点击共用方法
      preventClick(isClick) {
        if (!isClick) {
          this.isRendering = true
          setTimeout(() => {
            this.isRendering = false
          }, 500)
        }
      },
    },
  }
</script>

<style>
  .week_all {
    margin-top: 20rpx;
    display: flex;
    justify-content: space-evenly;
  }

  .week_btn {
    text-align: center;
    width: 90rpx;
    height: 90rpx;
    background-color: #0069b6;
    border-color: #0069b6;
    color: #fff;
    border-radius: 10rpx;
    line-height: 90rpx;
  }
</style>
